package com.tencent.miniproject.presenter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.tencent.miniproject.been.LoginSuccessBeen;
import com.tencent.miniproject.constants.Constant;
import com.tencent.miniproject.presenter.i.ILoginPresenter;
import com.tencent.miniproject.ui.MainActivity;
import com.tencent.miniproject.ui.i.ILoginActivity;
import com.tencent.miniproject.utils.GsonUtil;
import com.tencent.miniproject.utils.OkHttpResultCallback;
import com.tencent.miniproject.utils.OkHttpUtil;
import com.tencent.miniproject.utils.UserInfoUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class LoginPresenter implements ILoginPresenter {

    //view
    private ILoginActivity mView;

    public LoginPresenter(ILoginActivity activity) {
        super();
        mView = activity;
    }

    @Override
    public void login(final Context context, String access_token, String openid) {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", access_token);
        params.put("openid", openid);
        OkHttpUtil.getInstance().postAsync(Constant.SERVER_URL + "/user/login", new OkHttpResultCallback() {
            @Override
            public void onError(Call call, Exception e) {
                Log.i("ning", "onError: "  + call.toString());
            }

            @Override
            public void onResponse(byte[] bytes) {
                Log.i("ning", "onResponse: " + new String(bytes));
                try {
                    String string = new String(bytes);
                    LoginSuccessBeen loginSuccessBeen = GsonUtil.gsonToBean(string, LoginSuccessBeen.class);
                    if ("success".equals(loginSuccessBeen.getResult())){
                        context.getSharedPreferences("appBase", Context.MODE_PRIVATE).edit().putBoolean("loginSuccess", true).apply();
                        UserInfoUtil.setUserHeadUrl(context, loginSuccessBeen.getMessage().getHeadUrl());
                        UserInfoUtil.setUserName(context, loginSuccessBeen.getMessage().getUsername());
                        mView.jumpToMainActivity();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }, params);

//        Map<String, String> params = new HashMap<>();
//        params.put("access_token", access_token);
//        params.put("openid", openid);
//        OkHttpUtil.getInstance().getAsync(Constant.SERVER_URL + "/post/getFriendPosts?longitude=22.5&latitude=113.93&pageId=1", new OkHttpResultCallback() {
//            @Override
//            public void onError(Call call, Exception e) {
//                Log.i("ning", "onError: "  + call.toString());
//            }
//
//            @Override
//            public void onResponse(byte[] bytes) {
//                Log.i("ning", "onResponse: " + new String(bytes));
//            }
//        });

//        OkHttpUtil.getInstance().getAsync(Constant.SERVER_URL + "/post/getFriendPosts?longitude=22.5&latitude=113.93&pageId=1", new OkHttpResultCallback() {
//            @Override
//            public void onError(Call call, Exception e) {
//                Log.i("ning", "onError: ");
//            }
//
//            @Override
//            public void onResponse(byte[] bytes) {
//                Log.i("ning", "onResponse: ");
//            }
//        });
    }
}
